#include <sys/system.h>
#include <pages/pagectl.h>
#include <pages/page.h>
#include <gal/ptgui.h>
#include <gal/surface.h>

pt_int16_t x;
/*	Event Process Table	*/
static	void	ptgui_page_entry_handler( ptgui_page_entry_type_t Requese )
{	
	if( PTGUI_PAGE_PREPARE_FST == Requese ){
		//Do Nothing
	}
	else if ( PTGUI_PAGE_PREPARE_ALL == Requese ){
		x = 0;
	}
	else if ( PTGUI_PAGE_TERMINATE_ALL == Requese ){
		//Do Nothing
	}
	else if ( PTGUI_PAGE_TERMINATE_LST == Requese ){
	}
	else{
		//Do Nothing
	}	
}

static	ptgui_page_control_t nvdProcPsvUpdateEndCancel(pt_event_t* EventCode)
{
	return	PTGUI_PAGE_TRAN_CHANGE_REQUEST(FP_PAGE_BOOT);
}

static	ptgui_page_sw_state_t	nusGetViewPsvUpdateEndCancel(ptgui_page_switch_view_t *Switch)
{
	return	PAGE_SW_STAT_VISIBLE;
}

static void ptgui_page_render_handler(const struct ptgui_page_cell *page_cell)
{
	ptgui_surface_t *picture;
	ptgui_rect_t dest, update;
	ptgui_surface_t *screen = ptgui_surface_get_video_surface();
	char *bmpfile = "sample.bmp";		/* Sample image */

	picture = ptgui_surface_create_from_file(bmpfile);
	if ( picture == PT_NULL ) {
		pt_kprintf("Couldn't load %s: %s\n", bmpfile, ptgui_surface_get_error());
	}

	dest.y1 = (ptgui_surface_get_height(screen) - ptgui_surface_get_height(picture))/2;;
	dest.y2 = dest.y1 + ptgui_surface_get_height(picture);
	dest.x1 = x;
	dest.x2 = dest.x1 + ptgui_surface_get_width(picture);
	update = dest;

	if ( ptgui_surface_blit(picture, PT_NULL, screen, &update) < 0 ) {
		pt_kprintf("Blit failed: %s\n", ptgui_surface_get_error());
	}
	
	/* Free the picture and return */
	ptgui_surface_free(picture);
}

static void ptgui_page_frame_handler(const struct ptgui_page_cell *page_cell, pt_uint32_t delta)
{
	x += (pt_int16_t)delta / 10;
	if (x > ptgui_surface_get_width(ptgui_surface_get_video_surface()))
	{
		x = 0;
	}
}

static ptgui_page_control_t ptgui_page_key_handler(pt_event_t* message)
{
	ptgui_page_key_event_break();
	return PTGUI_PAGE_TRAN_RETURN_REQUEST(FP_PAGE_SIMPLE);
}

/*	Opening Cancel Key Definition	*/
PTGUI_PAGE_DEF_SW_VIEW( TswPsvUpdateEndCancel	,nvdProcPsvUpdateEndCancel ,nusGetViewPsvUpdateEndCancel	,0	,0	,0	,(KEY_ON_DO) );

/*	Static Key Table	*/
PTGUI_PAGE_BEGIN_REF_SW_VIEW_TABLE
	
PTGUI_PAGE_END_REF_SW_VIEW_TABLE(FP_PAGE_SIMPLE);

PTGUI_PAGE_BEGIN_DEF_EVENT_ACCEPT	
PTGUI_PAGE_DEF_EVENT_ACCEPT(EV_PAGECTL_KEY_ON, ptgui_page_key_handler),
PTGUI_PAGE_END_DEF_EVENT_ACCEPT;

PTGUI_PAGE_DEF_PAGECELL(FP_PAGE_SIMPLE, PAGE_FLAG_FULL_SCREEN);